//@author A0101906R
#include "CmdGCal.h"

using namespace Twentyfifth;
using namespace std;

const std::string CmdGCal::FEEDBACK_GCALOK = "Google Calendar set up successful.\n";
const std::string CmdGCal::FEEDBACK_GCAL_QUICKADD_SUCCESS = "quickAdd successful.\n";
const std::string CmdGCal::ERROR_UNDOGCAL = "GCal cannot be undone.\n";
const std::string CmdGCal::FEEDBACK_DELETED = "Undo quickAdd successful.\n";
//@author A0101906R
CmdGCal::CmdGCal() {
	userCommand = "";
	cmdType = GCAL;
}
//@author A0101119Y
Status CmdGCal::execution() {
	Status GCalStatus;
	assert(storage != NULL);
	//GCalAuthHandler GCalHandler;
	try {
		GCalStatus = storage->executeGCal(userCommand);
		if (!GCalStatus.tasks.empty()) {
			GCalStatus.code = Status::OK;
			task = GCalStatus.tasks[0];
			if (GCalStatus.desc == BLANK_STRING) {
				GCalStatus.desc = FEEDBACK_GCAL_QUICKADD_SUCCESS + task.toString();
			}
			GCalStatus.tasks.clear();
		}

	}
	catch (Poco::Exception &e) {
		GCalStatus.code = Status::ERR_INVALID_COMMAND;
		cerr << e.displayText() << endl;
	}

	return GCalStatus;
}
//@author A0101119Y
Status CmdGCal::undo(Status::StatusCode c) {
	Status s;
	if (task.getTaskType() != Task::INVALID) {
		assert(storage != NULL);
		s = storage->deleteTask(task.getId());
		s.code = Status::OK;
		s.desc = FEEDBACK_DELETED;
	} else {
		s.code = Status::ERR_OTHER;
		s.desc = ERROR_UNDOGCAL;
	}
	return s;
}
//@author A0101906R
void CmdGCal::setDescForGCal(Status& s) {

	if (s.code == Status::OK) {
		s.desc = FEEDBACK_GCALOK;
	} else {
		s.desc = statusCodeToString(s);
	}
	return;
}

void CmdGCal::setCommand(string command) {
	userCommand = command;
}